package edu.bmstu.rslab;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.awt.geom.Point2D;

/**
 * Created by Chikh (using IntelliJ IDEA).
* Date: 15.05.2009
* Time: 15:44:10
*/
class positioningAction extends AbstractAction {
    private final Camera camera;
    private final CoordinateSystemPanel coordinateSystemPanel;
    private final VideoPanel videoPanel;
    private final Guidance guidance;
    private final Detector detector;
    private final Detector directionDetector;
    private final Translator translator;

    public positioningAction(Camera camera, CoordinateSystemPanel coordinateSystemPanel, VideoPanel videoPanel,
                             Guidance guidance, Detector detector, Detector directionDetector, Translator translator) {
        super("Positioning");
        this.camera = camera;
        this.coordinateSystemPanel = coordinateSystemPanel;
        this.videoPanel = videoPanel;
        this.guidance = guidance;
        this.detector = detector;
        this.directionDetector = directionDetector;
        this.translator = translator;
    }

    public void actionPerformed(ActionEvent e) {
        BufferedImage video = camera.getImage();
        Point2D robotPosition = detector.detect(video),
                robotLCSPosition = translator.translate(robotPosition),
                robotDirection = directionDetector.detect(video),
                robotLCSDirection = translator.translate(robotDirection);
        coordinateSystemPanel.addRobotPositionPoint(robotLCSPosition);
        videoPanel.setDetectedPosition(robotPosition);
        videoPanel.setDetectedDirection(robotDirection);
        guidance.sendControlSignal(robotLCSPosition, robotLCSDirection);
    }
}
